package com.appkubes.mall.admin.application.plugin.rent.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.appkubes.common.utils.net.HttpClientUtils;
import com.appkubes.mall.admin.application.plugin.rent.service.PluginRentService;
import com.appkubes.mall.admin.application.plugin.rent.service.PluginRentUrlService;
import lombok.extern.java.Log;
import okhttp3.Response;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
@Log
public class PluginRentServiceImpl implements PluginRentService {
    @Resource
    PluginRentUrlService pluginRentUrlService;

    public JSONObject getOrderState(String orderKey) throws RuntimeException {
        Map<String, Object> map = new HashMap<>();
        map.put("orderKey", orderKey);

        try {
            Response response = HttpClientUtils.get(pluginRentUrlService.getQueryRentOrderURL(), map);
            String str = response.body().string();
            if (!StringUtils.isEmpty(str)) {
                JSONObject jsonObject = JSON.parseObject(str);
                if (jsonObject.getInteger("code") != 200) {
                    throw new RuntimeException(jsonObject.getString("message"));
                }
                return jsonObject.getJSONObject("data");
            }
        } catch (Exception e) {
            throw new RuntimeException("租赁单查询失败");
        }
        throw new RuntimeException("租赁单查询失败");
    }
}
